﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.Data.Entity;
using TomNet.IMSC.Model.DbModels.NewHouse;
using TomNet.IMSC.Web.Common;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.LowPriceHouse.Controllers
{
    public class LowPriceHouseStatisticsController : BaseController
    {
        DefaultDbContext DbContext = new DefaultDbContext();
        // GET: LowPriceHouse/LowPriceHouseStatistics
        public ActionResult LowPriceHouseBuildingIndex()
        {
            return View();
        }
        public ActionResult LowPriceHouseBuildingIndexAynce(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate)
        {
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            var model = Common.CookiesManagement.GetLoginModel(ticket);
            //获取权限
            //LoginModel login = CookiesManagement.GetLoginModel(CookiesManagement.GetTicket());            
            var loginModel = CookiesManagement.GetLoginModel(ticket);

            SqlParameter[] parameters = { new SqlParameter("@StaffID", loginModel.Id), new SqlParameter("@BeginTime", beginDate), new SqlParameter("@EndTime", endDate) };

            var query = DbContext.SqlQuery<Buildings>("select * from [dbo].[Fn_Table_Report_Decoration4DecorationCompany](@id,@BeginTime,@EndTime)", parameters);


            var total = query.Count();

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<Buildings>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<Buildings> gridData = new GridData<Buildings>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
    }
}